package com.nbcio.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nbcio.common.core.utils.MapstructUtils;
import com.nbcio.common.core.utils.StringUtils;
import com.nbcio.iot.domain.entity.IotDevice;
import com.nbcio.iot.domain.bo.IotDeviceBo;
import com.nbcio.iot.domain.vo.IotDeviceVo;
import com.nbcio.iot.mapper.IotDeviceMapper;
import com.nbcio.iot.service.IIotDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 设备Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class IotDeviceServiceImpl implements IIotDeviceService {

    private final IotDeviceMapper baseMapper;

    @Override
    public IotDeviceVo queryById(Long deviceId) {
        return baseMapper.selectVoById(deviceId);
    }

    @Override
    public List<IotDeviceVo> queryList(IotDeviceBo bo) {
        IotDevice build = MapstructUtils.convert(bo, IotDevice.class);
        return baseMapper.selectDeviceVoList(build);
    }

    @Override
    public List<IotDeviceVo> queryListByGroupId(Long groupId) {
        return baseMapper.selectDeviceVoListByGroupId(groupId);
    }

    @Override
    public Boolean insertByBo(IotDeviceBo bo) {
        IotDevice add = MapstructUtils.convert(bo, IotDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDeviceId(add.getDeviceId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(IotDeviceBo bo) {
        IotDevice update = MapstructUtils.convert(bo, IotDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean checkDeviceNameUnique(IotDeviceBo bo) {
        Long deviceId = StringUtils.isNull(bo.getDeviceId()) ? -1L : bo.getDeviceId();
        LambdaQueryWrapper<IotDevice> qw = new LambdaQueryWrapper<>();
        qw.eq(IotDevice::getDeviceName, bo.getDeviceName());
        IotDevice info = baseMapper.selectOne(qw);
        if (StringUtils.isNotNull(info) && info.getDeviceId().longValue() != deviceId.longValue()) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean checkDeviceCodeUnique(IotDeviceBo bo) {
        Long deviceId = StringUtils.isNull(bo.getDeviceId()) ? -1L : bo.getDeviceId();
        LambdaQueryWrapper<IotDevice> qw = new LambdaQueryWrapper<>();
        qw.eq(IotDevice::getDeviceCode, bo.getDeviceCode());
        IotDevice info = baseMapper.selectOne(qw);
        if (StringUtils.isNotNull(info) && info.getDeviceId().longValue() != deviceId.longValue()) {
            return false;
        }
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotDevice entity) {
        // 做一些数据校验,如唯一约束
    }
} 